package com.sgcc.pda.hzaidl.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.RemoteException;
import android.text.TextUtils;


import com.sgcc.pda.data.entity.PushOfflineMsg;
import com.sgcc.pda.tools.JsonUtils;
import com.sgcc.pda.tools.LogPrintUtils;
import com.sgcc.platforminterface.HttpManager;


/**
 * @创建者：suven on 2019/9/4 13:26
 * @描述：推送消息
 * @版本：1.0
 * @修改人：
 * @修改时间：
 * @修改说明：
 * @修改版本：
 */
public class PushReceiver extends BroadcastReceiver {


    public String ACTION_FILTER = "web.mqtt.notification.";


    PushReceiverCallBack pushReceiverCallBack;

    public interface PushReceiverCallBack {
        void onReceive(Intent intent, String topic, String content);
    }

    String mPackageName;

    /**
     * 注册广播
     */
    public PushReceiver(Context context, String packageName, String... topic) {
        mPackageName = packageName;
        IntentFilter intentFilter = new IntentFilter();
        for (int i = 0; i < topic.length; i++) {
            try {
                LogPrintUtils.d(String.format("mqtt:订阅前%s  %s", topic[i], packageName));
                boolean b = HttpManager.subscribe(packageName, topic[i]);
                LogPrintUtils.d(String.format("mqtt:订阅后%s  %s", topic[i], b));
            } catch (RemoteException e) {
                e.printStackTrace();
                LogPrintUtils.d(String.format("mqtt:订阅%s异常  %s", topic[i], packageName), e);
            }
            intentFilter.addAction(ACTION_FILTER + topic[i]);
        }
        context.registerReceiver(this, intentFilter, null, null);
    }

    public PushReceiverCallBack getPushReceiverCallBack() {
        return pushReceiverCallBack;
    }

    public void setPushReceiverCallBack(PushReceiver.PushReceiverCallBack pushReceiverCallBack) {
        this.pushReceiverCallBack = pushReceiverCallBack;
    }

    /**
     * 接收广播内容
     *
     * @param context
     * @param intent
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            LogPrintUtils.d(String.format("mqtt:收到 action%s   %s", intent.getAction(), intent.getStringExtra("content")));
            if (intent.getAction().contains(ACTION_FILTER)) {
                String action = intent.getAction();
                String content = intent.getStringExtra("content");
                LogPrintUtils.d(String.format("mqtt:接收 action%s   %s", action, content));
                if (!TextUtils.isEmpty(content)) {
                    PushOfflineMsg pushOfflineMsg = JsonUtils.toBean(content, PushOfflineMsg.class);
                    if (pushOfflineMsg != null && !TextUtils.isEmpty(pushOfflineMsg.getTimeTag())) {
                        boolean bool = HttpManager.reportReceivedMsg(mPackageName, pushOfflineMsg.getTimeTag());
                        LogPrintUtils.d(String.format("mqtt:上报结果：%s  %s", bool, pushOfflineMsg.getTimeTag()));
                    }
                }
                if (pushReceiverCallBack != null) {
                    pushReceiverCallBack.onReceive(intent, action.substring(ACTION_FILTER.length()), content);
                }
            }
        } catch (Exception e) {
            LogPrintUtils.d(String.format("mqtt：推送接收产生异常：%s", e));
        }
    }


}
